﻿using Engine;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ThinkDotNetEngine;
using ThinkDotNetEngine.Bots;

namespace ThinkDotNetEngineDrawer.Bots
{
    public class BotsStatusDrawer:DrawableGameComponent
    {
        public BotsStatusDrawer(Game game) : base(game)
        {
            DrawOrder = int.MaxValue;
        }
        private SpriteBatch _spriteBatch;
        private SpriteFont _botTextFont;

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);  
            _botTextFont = Game.Content.Load<SpriteFont>("Fonts\\BotText");
            base.LoadContent();
        }
        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();

            for (int i = 0; i < Arena.Bots.Count; i++)
            {
                TankBot tankbot = Arena.Bots[i];
                const int leftMargin = 50;
                const int rowHeight = 20;
                int columnIndex = i%2;
                int rowIndex = i/2;

                DrawBotStatus(tankbot, new Vector2(columnIndex*GraphicsDevice.Viewport.Width/2 + leftMargin, rowIndex*rowHeight));
            }
            _spriteBatch.End();

            base.Draw(gameTime);
        }
        private Texture2D GetHealthStatusTexture(TankBot tankBot)
        {
            Point healthStatusSize = new Point(200, 12);

            Texture2D texture2D = new Texture2D(GraphicsDevice, healthStatusSize.X, healthStatusSize.Y);
            Color[] bytes = new Color[texture2D.Width*texture2D.Height];
            float heathStatus = tankBot.CurrentHealth/TankBot.MaxHealth;
            for (int i = 0; i < texture2D.Height; i++)
            {
                int greenPixels = (int) (texture2D.Width*heathStatus);

                for (int j = 0; j < greenPixels; j++)
                {
                    byte greenValue = (byte)MathHelper.Lerp(20, 255, 1f - (float) j/greenPixels);
                    bytes[i * texture2D.Width + j] = new Color(0, 255, 0, greenValue);
                }

                for (int j = greenPixels; j < texture2D.Width; j++)
                    bytes[i*texture2D.Width + j] = new Color(255, 0, 0, 20);
            }
            texture2D.SetData(bytes);

            return texture2D;
        }
        private void DrawBotStatus(TankBot tankBot, Vector2 pos)
        {            
            string botName = "NO_NAME";
            const int maxBotNameLength = 10;
            if (tankBot.Bot.Metadata != null && !string.IsNullOrEmpty(tankBot.Bot.Metadata.Name))
                botName = tankBot.Bot.Metadata.Name;

            botName = botName.Substring(0, botName.Length > maxBotNameLength ? maxBotNameLength : botName.Length);            

            Vector2 textSize = _botTextFont.MeasureString(botName);
            const int textMargin = 10;
            Texture2D healthTexture = GetHealthStatusTexture(tankBot);

            Rectangle dest = new Rectangle((int) pos.X + (int) textSize.X + textMargin,
                                           (int) pos.Y + (int) (textSize.Y/2)-healthTexture.Height/2, healthTexture.Width,
                                           healthTexture.Height);

            _spriteBatch.DrawString(_botTextFont, botName, pos, Color.Black);
            _spriteBatch.Draw(healthTexture, dest, Color.White);
        }

        public Arena Arena { get { return (Arena)Game.Services.GetService(typeof(World)); } }
    }
}
